//+------------------------------------------------------------------+
//|                                                   PriceModule.mqh |
//|                                      价格模块 - 负责价格数据管理   |
//|                                                                  |
//+------------------------------------------------------------------+
#property strict

//+------------------------------------------------------------------+
//| 价格数据结构体                                                    |
//+------------------------------------------------------------------+
struct PriceData
{
   double            bid;               // 买价
   double            ask;               // 卖价
   double            spread;            // 点差
   double            spreadPoints;      // 点差点数
   datetime          updateTime;       // 更新时间
   bool              isValid;          // 数据是否有效
};

//+------------------------------------------------------------------+
//| 价格模块类                                                        |
//+------------------------------------------------------------------+
class CPriceModule
{
private:
   string            m_symbol;          // 交易品种
   PriceData         m_currentPrice;    // 当前价格数据
   PriceData         m_lastPrice;       // 上次价格数据
   datetime          m_lastUpdate;      // 上次更新时间
   int               m_updateInterval;  // 更新间隔(秒)
   
   // 价格统计
   double            m_maxSpread;       // 最大点差
   double            m_minSpread;       // 最小点差
   double            m_avgSpread;       // 平均点差
   int               m_priceCount;      // 价格更新次数
   
public:
   // 构造函数
   CPriceModule(string symbol = "", int updateInterval = 1);
   
   // 析构函数
   ~CPriceModule();
   
   // 更新价格数据
   bool              UpdatePrice();
   
   // 获取当前价格数据
   PriceData         GetCurrentPrice() { return m_currentPrice; }
   
   // 获取买价
   double            GetBid() { return m_currentPrice.bid; }
   
   // 获取卖价
   double            GetAsk() { return m_currentPrice.ask; }
   
   // 获取点差
   double            GetSpread() { return m_currentPrice.spread; }
   
   // 获取点差点数
   double            GetSpreadPoints() { return m_currentPrice.spreadPoints; }
   
   // 检查价格是否需要更新
   bool              NeedUpdate();
   
   // 检查点差是否可接受
   bool              IsSpreadAcceptable(double maxSpread = 5.0);
   
   // 获取市场信息
   double            GetMarketInfo(int type);
   
   // 计算价格变化
   double            GetPriceChange();
   
   // 获取点差统计
   double            GetMaxSpread() { return m_maxSpread; }
   double            GetMinSpread() { return m_minSpread; }
   double            GetAvgSpread() { return m_avgSpread; }
   
   // 重置统计数据
   void              ResetStatistics();
   
   // 打印价格信息
   void              PrintPriceInfo();
   
   // 设置更新间隔
   void              SetUpdateInterval(int seconds) { m_updateInterval = seconds; }
   
private:
   // 初始化价格数据
   void              InitializePriceData();
   
   // 更新统计数据
   void              UpdateStatistics();
   
   // 验证价格数据
   bool              ValidatePriceData(const PriceData &price);
};

//+------------------------------------------------------------------+
//| 构造函数                                                          |
//+------------------------------------------------------------------+
CPriceModule::CPriceModule(string symbol = "", int updateInterval = 1)
{
   m_symbol = (symbol == "") ? Symbol() : symbol;
   m_updateInterval = updateInterval;
   m_lastUpdate = 0;
   m_priceCount = 0;
   
   InitializePriceData();
   ResetStatistics();
   
   // 首次更新价格
   UpdatePrice();
}

//+------------------------------------------------------------------+
//| 析构函数                                                          |
//+------------------------------------------------------------------+
CPriceModule::~CPriceModule()
{
   // 清理资源
}

//+------------------------------------------------------------------+
//| 更新价格数据                                                      |
//+------------------------------------------------------------------+
bool CPriceModule::UpdatePrice()
{
   // 保存上次价格
   m_lastPrice = m_currentPrice;
   
   // 获取新价格数据
   m_currentPrice.bid = MarketInfo(m_symbol, MODE_BID);
   m_currentPrice.ask = MarketInfo(m_symbol, MODE_ASK);
   m_currentPrice.spread = m_currentPrice.ask - m_currentPrice.bid;
   m_currentPrice.spreadPoints = m_currentPrice.spread / Point;
   m_currentPrice.updateTime = TimeCurrent();
   m_currentPrice.isValid = true;
   
   // 验证价格数据
   if(!ValidatePriceData(m_currentPrice))
   {
      m_currentPrice.isValid = false;
      return false;
   }
   
   // 更新统计数据
   UpdateStatistics();
   
   m_lastUpdate = TimeCurrent();
   m_priceCount++;
   
   return true;
}

//+------------------------------------------------------------------+
//| 检查是否需要更新                                                  |
//+------------------------------------------------------------------+
bool CPriceModule::NeedUpdate()
{
   return (TimeCurrent() - m_lastUpdate >= m_updateInterval);
}

//+------------------------------------------------------------------+
//| 检查点差是否可接受                                                |
//+------------------------------------------------------------------+
bool CPriceModule::IsSpreadAcceptable(double maxSpread = 5.0)
{
   if(!m_currentPrice.isValid) return false;
   
   return (m_currentPrice.spreadPoints <= maxSpread);
}

//+------------------------------------------------------------------+
//| 获取市场信息                                                      |
//+------------------------------------------------------------------+
double CPriceModule::GetMarketInfo(int type)
{
   return MarketInfo(m_symbol, type);
}

//+------------------------------------------------------------------+
//| 计算价格变化                                                      |
//+------------------------------------------------------------------+
double CPriceModule::GetPriceChange()
{
   if(!m_currentPrice.isValid || !m_lastPrice.isValid)
      return 0;
   
   double midPrice = (m_currentPrice.bid + m_currentPrice.ask) / 2.0;
   double lastMidPrice = (m_lastPrice.bid + m_lastPrice.ask) / 2.0;
   
   return midPrice - lastMidPrice;
}

//+------------------------------------------------------------------+
//| 重置统计数据                                                      |
//+------------------------------------------------------------------+
void CPriceModule::ResetStatistics()
{
   m_maxSpread = 0;
   m_minSpread = 999999;
   m_avgSpread = 0;
   m_priceCount = 0;
}

//+------------------------------------------------------------------+
//| 打印价格信息                                                      |
//+------------------------------------------------------------------+
void CPriceModule::PrintPriceInfo()
{
   if(!m_currentPrice.isValid)
   {
      Print("价格数据无效");
      return;
   }
   
   Print("=== 价格信息 ===");
   Print("品种: ", m_symbol);
   Print("买价: ", DoubleToString(m_currentPrice.bid, Digits));
   Print("卖价: ", DoubleToString(m_currentPrice.ask, Digits));
   Print("点差: ", DoubleToString(m_currentPrice.spreadPoints, 1), " 点");
   Print("更新时间: ", TimeToString(m_currentPrice.updateTime));
   Print("点差统计 - 最大: ", DoubleToString(m_maxSpread, 1), 
         ", 最小: ", DoubleToString(m_minSpread, 1), 
         ", 平均: ", DoubleToString(m_avgSpread, 1));
}

//+------------------------------------------------------------------+
//| 初始化价格数据                                                    |
//+------------------------------------------------------------------+
void CPriceModule::InitializePriceData()
{
   m_currentPrice.bid = 0;
   m_currentPrice.ask = 0;
   m_currentPrice.spread = 0;
   m_currentPrice.spreadPoints = 0;
   m_currentPrice.updateTime = 0;
   m_currentPrice.isValid = false;
   
   m_lastPrice = m_currentPrice;
}

//+------------------------------------------------------------------+
//| 更新统计数据                                                      |
//+------------------------------------------------------------------+
void CPriceModule::UpdateStatistics()
{
   if(!m_currentPrice.isValid) return;
   
   double currentSpreadPoints = m_currentPrice.spreadPoints;
   
   // 更新最大最小点差
   if(currentSpreadPoints > m_maxSpread)
      m_maxSpread = currentSpreadPoints;
   
   if(currentSpreadPoints < m_minSpread)
      m_minSpread = currentSpreadPoints;
   
   // 更新平均点差
   if(m_priceCount > 0)
   {
      m_avgSpread = (m_avgSpread * m_priceCount + currentSpreadPoints) / (m_priceCount + 1);
   }
   else
   {
      m_avgSpread = currentSpreadPoints;
   }
}

//+------------------------------------------------------------------+
//| 验证价格数据                                                      |
//+------------------------------------------------------------------+
bool CPriceModule::ValidatePriceData(const PriceData &price)
{
   // 检查价格是否为正数
   if(price.bid <= 0 || price.ask <= 0)
   {
      Print("错误: 价格数据无效 - Bid: ", price.bid, ", Ask: ", price.ask);
      return false;
   }
   
   // 检查买价是否小于卖价
   if(price.bid >= price.ask)
   {
      Print("错误: 买价大于等于卖价 - Bid: ", price.bid, ", Ask: ", price.ask);
      return false;
   }
   
   // 检查点差是否合理
   if(price.spreadPoints > 100) // 点差超过100点可能有问题
   {
      Print("警告: 点差过大 - ", price.spreadPoints, " 点");
   }
   
   return true;
}